package com.amap.activity;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.amap.R;
import com.amap.adapter.FoodAdapter;
import com.amap.banco.Banco;
import com.amap.model.Food;

public class MenuActivity extends Activity implements OnItemClickListener{
	
	FoodAdapter foodAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		
		ListView listView = (ListView) findViewById(R.id.list);
		listView.setTextFilterEnabled(true);
		listView.setOnItemClickListener(this);
		
		foodAdapter = new FoodAdapter(this);
		listView.setAdapter(foodAdapter);

		//Button Event
		ImageButton total = (ImageButton) findViewById(R.id.imageButton1);
		total.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent();
				i.setClass(v.getContext(), LocationListView.class);
			    i.putExtra("nameUrl", "");
				startActivity(i);	
			}
		});

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Banco.IncrementaPreferencias(((Food) foodAdapter.getItem(arg2)).getName());
		Intent i = new Intent();
        i.setClass(arg1.getContext(), LocationListView.class);	 
        i.putExtra("nameUrl", ((Food) foodAdapter.getItem(arg2)).getNameUrl());
 		startActivity(i);

	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		updtadeList();
	}
	
	private void updtadeList(){
			List<Food> foods =  new ArrayList<Food>();
			
			Food food;
			
			Cursor foodCursor = Banco.ConsultaPreferencias ();		
			foodCursor.moveToFirst();

			for( int i = 0; i < foodCursor.getCount(); i++){
				food = new Food(foodCursor.getString(0), foodCursor.getInt(1), foodCursor.getString(2));
				foods.add(food); 
				if(!foodCursor.isLast()){
					foodCursor.moveToNext();
				}
			}	
			
			foodAdapter.setFoods(foods);
		    foodAdapter.notifyDataSetChanged();
	}

}
